package com.lucenly.card.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lucenly.card.R;
import com.lucenly.card.adapter.MsgAdapter;
import com.lucenly.card.base.BaseActivity;
import com.lucenly.card.bean.MsgBean;
import com.lucenly.card.constants.UrlDefend;
import com.lucenly.card.event.StringMsgEvent;
import com.lucenly.card.utils.GsonUtil;
import com.lucenly.card.view.refresh2.RefreshLayout;
import com.lucenly.card.view.refresh2.SmartRefreshLayout;
import com.lucenly.card.view.refresh2.listener.OnLoadMoreListener;
import com.lucenly.card.view.refresh2.listener.OnRefreshListener;
import com.siberiadante.toastutils.ToastUtil;
import com.vise.xsnow.event.BusManager;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;
import com.vise.xsnow.utls.SharedPreferencesUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import ezy.ui.layout.LoadingLayout;

public class MsgListActivity extends BaseActivity {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.ll_back)
    LinearLayout ll_back;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    MsgAdapter adapter;
    @BindView(R.id.view)
    LoadingLayout view;
    List<MsgBean> list = new ArrayList<>();
    int page = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_msg_list);
    }

    @Override
    protected void initView() {
        ll_back.setVisibility(View.VISIBLE);
        tv_title.setText("消息");
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new MsgAdapter();
        adapter.replaceData(list);
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void bindEvent() {
        ll_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page=1;
                getData();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getData();
            }
        });
    }

    @Override
    protected void initData() {
        getData();
        read();
    }

    private void read() {
        ViseHttp.POST(UrlDefend.READ)
                .addHeader("Token", SharedPreferencesUtil.getInstance().getString("Token"))
                .request(new ACallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        BusManager.getBus().post(new StringMsgEvent("read"));
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {

                    }
                });
    }

    private void getData() {
        ViseHttp.GET(UrlDefend.MSG_LIST)
                .addHeader("Token", SharedPreferencesUtil.getInstance().getString("Token"))
                .addParam("page",page+"")
                .request(new ACallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        refreshLayout.finishRefresh();
                        refreshLayout.finishLoadMore();
                        try {
                            JSONObject jsonObject = new JSONObject(data);
                            if(jsonObject.has("url")){
                                String path = jsonObject.getString("url");
                                adapter.setPath(path);
                            }
                            JSONArray jsonArray = jsonObject.getJSONArray("list");
                            if(jsonArray!=null&&jsonArray.length()>0){
                                ArrayList<MsgBean> msgBeans = GsonUtil.fromJsonList(jsonArray.toString(), MsgBean.class);
                                if(msgBeans!=null&&msgBeans.size()>0){
                                    if(page==1){
                                        adapter.replaceData(msgBeans);
                                    }else{
                                        adapter.addData(msgBeans);
                                    }
                                    view.showContent();
                                }else{
                                    if(page==1){
                                        view.showEmpty();
                                    }else{
                                        view.showContent();
                                        ToastUtil.showSingletonShort("暂无更多消息");
//                                        Toast.makeText(mContext, "暂无更多消息", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            }else{
                                if(page==1){
                                    view.showEmpty();
                                }else{
                                    view.showContent();ToastUtil.showSingletonShort("暂无更多消息");
//                                    Toast.makeText(mContext, "暂无更多消息", Toast.LENGTH_SHORT).show();
                                }
                            }
                        } catch (JSONException e) {
                            view.setErrorText(e.getMessage()+"");
                            view.showError();
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        refreshLayout.finishRefresh();
                        refreshLayout.finishLoadMore();ToastUtil.showSingletonShort(errMsg);
//                        Toast.makeText(mContext, errMsg, Toast.LENGTH_SHORT).show();
                        view.setErrorText(errMsg);
                        view.showError();
                    }
                });
    }

    @Override
    protected void processClick(View view) {

    }
}
